package com.sky.web.app;

import com.sky.context.ThreadLocalUtil;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user/shoppingCart")
public class AppShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("list")
    public Result findListByShoppingCart(){
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(ThreadLocalUtil.getCurrentId())
                .build();
       List<ShoppingCart> list = shoppingCartService.findListByShoppingCart(shoppingCart);
        return Result.success(list);
    }

    @PostMapping("add")
    public Result addShoppingCart(@RequestBody ShoppingCartDTO dto){
        shoppingCartService.addShoppingCart(dto);

        return Result.success();
    }

    @DeleteMapping("clean")
    public Result cleanShoppingCart(){
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }

    @PostMapping("sub")
    public Result subShoppingCart(@RequestBody ShoppingCartDTO dto){
        shoppingCartService.subShoppingCart(dto);

        return Result.success();
    }
}
